package 剑指offer_leetcode;

/**
 * JZ53数字在升序数组中出现的次数
 */
public class 剑指Offer53_I_在排序数组中查找数字1 {
    public int search(int[] nums, int target) {
        return MidSort(nums,target+0.5) - MidSort(nums,target-0.5);
    }

    int MidSort(int[] nums, double k){
        int left = 0;
        int right = nums.length-1;
        while(left<=right){
            int mid = (left+right) / 2;
            if(nums[mid]>k)
                right = mid - 1;
            else if(nums[mid]<k)
                left = mid + 1;
        }
        return left;
    }
}
